package com.zjb.demo.system.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.mybatisflex.core.paginate.Page;
import com.zjb.demo.common.domain.dto.SysLoginInfo;
import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.system.domain.SysConfig;
import com.zjb.demo.system.domain.query.SysConfigQuery;
import com.zjb.demo.system.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.Valid;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author 庄佳彬
 * @since 2023/12/27 14:28
 */
@Valid
@Controller
@Api(tags = "参数配置")
@Mapping("/system/config")
public class SysConfigController {

    @Inject
    private SysConfigService sysConfigService;

    @Get
    @Mapping("/list")
    @ApiOperation(value = "参数配置列表")
    public AjaxResult<Page<SysConfig>> list(SysConfigQuery query) {
        return AjaxResult.success(sysConfigService.pageList(query));
    }

    @Get
    @Mapping("/export")
    @ApiOperation(value = "参数配置导出")
    public void export(SysConfigQuery query) throws IOException {
        List<SysConfig> list = sysConfigService.list(query);
        var ctx = Context.current();
        ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ctx.headerAdd("Content-Disposition", "attachment;filename=" + URLEncoder.encode("配置", StandardCharsets.UTF_8) + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx");
        var os = ctx.outputStream();
        EasyExcel.write(os, SysLoginInfo.class)
                .sheet("配置")
                .doWrite(list);
    }

    @Get
    @Mapping("/{configId}")
    @ApiOperation(value = "根据参数编号获取详细信息")
    public AjaxResult<SysConfig> detail(Long configId) {
        return AjaxResult.success(sysConfigService.getById(configId));
    }

    @Get
    @Mapping("/configKey/{configKey}")
    @ApiOperation(value = "根据参数键名查询参数值")
    public AjaxResult<String> detailByKey(String configKey) {
        return AjaxResult.success(sysConfigService.getValueByKey(configKey));
    }

    @Post
    @Mapping
    @ApiOperation(value = "保存参数配置")
    public AjaxResult<Boolean> save(@Body SysConfig sysConfig) {
        if (sysConfigService.checkConfigKeyUnique(sysConfig)) {
            return AjaxResult.fail("参数键名已存在");
        }
        return AjaxResult.success(sysConfigService.saveAndCache(sysConfig));
    }

    @Put
    @Mapping
    @ApiOperation(value = "修改参数配置")
    public AjaxResult<Boolean> update(@Body SysConfig sysConfig) {
        if (sysConfigService.checkConfigKeyUnique(sysConfig)) {
            return AjaxResult.fail("参数键名已存在");
        }
        return AjaxResult.success(sysConfigService.updateAndCache(sysConfig));
    }

    @Delete
    @Mapping("/{configIds}")
    @ApiOperation(value = "删除参数配置")
    public AjaxResult<Void> delete(Long[] configIds) {
        sysConfigService.removeByIdsAndCache(configIds);
        return AjaxResult.success();
    }

    @Delete
    @Mapping("/refreshCache")
    @ApiOperation(value = "刷新缓存")
    public AjaxResult<Void> refreshCache() {
        sysConfigService.refreshCache();
        return AjaxResult.success();
    }
}
